<template>
  <div class="article-container">
    <!-- 导航栏 -->
    <van-nav-bar
      fixed
      left-arrow
      @click-left="$router.back()"
      title="文章详情"
    ></van-nav-bar>
    <!-- /导航栏 -->

    <!-- 文章详情 -->
    <div class="detail">
      <h3 class="title">{{article.title}}</h3>
      <div class="author">
        <van-image round width="1rem" height="1rem" fit="fill" :src="article.aut_photo" />
        <div class="text">
          <p class="name">{{article.aut_name}}</p>
          <p class="time">{{article.pubdate | fromNow}}</p>
        </div>
        <van-button round size="small" type="info" @click="followClick">
            {{ article.is_followed === true ? '已关注' :'+关注'}}
            </van-button>
      </div>
      <div class="content">
        <div v-html="article.content">正文</div>
      </div>
      <br />
      <br />
      <div class="zan">
        <van-button
          round
          size="small"
          hairline
          type="primary"
          plain
          :icon="article.attitude == 1? 'good-job': 'good-job-o'"
          @click="likeClick"
          >{{ article.attitude ==1 ? '取消点赞':'点赞'}}</van-button
        >
        &nbsp;&nbsp;&nbsp;&nbsp;
        <van-button
          round
          size="small"
          hairline
          type="danger"
          plain
          :icon="article.attitude ==0?'delete':'delete-0'"
          @click="keyClick"
          >{{article.attitude ==0 ? '取消不喜欢':'不喜欢'}}
          </van-button>
      </div>
    </div>
    
  </div>
</template>

<script>
// 导入接口
import {getArticleInfo,addFollowing,removeFollowing,addLikings,removeLikings, addkey,removekey} from './home'
export default {
  name: 'myarticle',
  data () {
    return {
      article: {}
    }
  },
  async created() {
      let res = await getArticleInfo(this.$route.query.id)
      console.log(res);
      this.article = res.data.data
      
  },
  methods: {
    async followClick () {
      // 已关注,要取消关注
      if (this.article.is_followed) {
        let res = await removeFollowing(this.article.aut_id)
        // 弹窗提示
        this.$toast.success('取消关注成功')
        // 改变状态
        this.article.is_followed = false
        // 未关注，要关注
      } else {
        let res = await addFollowing(this.article.aut_id)
        // 弹窗提示
        this.$toast.success('关注成功')
        // 改变状态
        this.article.is_followed = true
      }
    },
    async likeClick(){
        if(this.article.attitude == 1){
            //当前点赞 点击取消点赞
            let res = await removeLikings(this.article.art_id)
            //弹窗提示
             this.$toast.success('取消点赞成功')
            //修改当前点赞状态
            this.article.attitude = -1
        }else{
            //当前没有点赞 点击点赞
            let res = await addLikings(this.article.art_id)
            // 弹窗提示
            this.$toast.success('点赞成功')
            // 修改当前点赞状态
            this.article.attitude = 1
        }
    },
    async keyClick(){
        if(this.article.attitude == 0){
            //当前不喜欢 点击取消不喜欢
            let res = await removekey(this.article.art_id)
            //弹窗提示
             this.$toast.success('取消点赞成功')
            //修改当前不喜欢状态
            this.article.attitude = -1
        }else{
            //当前没有不喜欢 点击不喜欢
            let res = await addkey(this.article.art_id)
            // 弹窗提示
            this.$toast.success('点赞成功')
            // 修改当前不喜欢状态
            this.article.attitude = 0
        }
    }
  }
}
</script>

<style lang="less" scoped>
.article-container {
  position: absolute;
  left: 0;
  top: 0;
  overflow-y: scroll;
  width: 100%;
  height: 100%;
}
.article-loading {
  padding-top: 100px;
  text-align: center;
}
.error {
  padding-top: 100px;
  text-align: center;
}
.detail {
  padding: 50px 10px;
  .title {
    font-size: 16px;
  }
  .zan {
    text-align: center;
  }
  .author {
    padding: 10px 0;
    display: flex;
    .text {
      flex: 1;
      padding-left: 10px;
      line-height: 1.3;
      .name {
        font-size: 14px;
        margin: 0;
      }
      .time {
        margin: 0;
        font-size: 12px;
        color: #999;
      }
    }
  }
  .content {
    font-size: 14px;
    overflow: hidden;
    white-space: pre-wrap;
    word-break: break-all;
    img {
      max-width: 100%;
      background: #f9f9f9;
    }
  }
}
</style>
